$(document).ready(initExtendedSearch);

function initExtendedSearch(){
    var inputBox = "#j_idt10"+'\\:'+"txtData"; // haxx-solution since the primefaces fuck with me!
    if( $(inputBox) != undefined )
        $(inputBox).attachExtendedSearch();
}

/* 
 * author:  Johan
 * Todo:    Might be better with som refactoring of the init function.
 * Purpose: To display a jQuery addOn function.
 *          Makes a extended search box, with searchresults
 *          while typing( like new google function or vasttrafik-search )
 */
jQuery.fn.attachExtendedSearch = function( boxClassName ) {
    //Rigging up som variables
    var inputBox = this;
    var searchString = "";
    inputBox.val(searchString);
    if( boxClassName == undefined || boxClassName == "" )
        boxClassName = "divExtendedIx"+Math.round(Math.random()*10000);

    //Setting up the div for the search results
    inputBox.parent().append("<div id   =\""+boxClassName+"\"></div>");
    var searchDiv = $("#"+boxClassName);
    var boxX = inputBox.offset().left+15; //Position values => primefaces haxx
    var boxY = inputBox.offset().top+inputBox.outerHeight()*4-4;
    searchDiv.offset({left: boxX, top: boxY});
    searchDiv.css('display','none');
    searchDiv.css('position','absolute');
    searchDiv.css('float','left');
    searchDiv.css('border', 'solid 1px');
    searchDiv.css('border-color', '#FFFFFF');
    searchDiv.css('background-color','#000000');
    searchDiv.css('color', '#FFFFFF');
    searchDiv.css('width', inputBox.outerWidth() );

    //Functionality
    inputBox.focusin(function(){ showDiv(); });
    inputBox.focusout(function(){ hideDiv(); });
    inputBox.keyup(function(){
        if( searchString != inputBox.val() ){
            var oldSearch = searchString;
            searchString = inputBox.val();
            inputBox.val(searchString); //So they're always the same!
            //only if new searchString is longer than the first.
            if( searchString != oldSearch ){
                searchDiv.load("torrentSlimList.jsf",{q:searchString, mode:"slim"},function(response, status, xhr){
                    if( status != "error" ){
                        //Display the results here
                        searchDiv.html(response);
                        //Set up click-functionality if diffrent results
                        $("#torrentList").css('list-style','none');
                        $("#torrentList").removeAttr("style");
                        $("#torrentList").css('border','solid 1px');
                        $("#torrentList li").css('left','0');
                        $("#torrentList li").css('border-color','#FFFFFF');
                        $("#torrentList").css('align','left');
                        $("#torrentList li").each(function() {
                            if( inputBox.val() != $(this).text() ){
                                $(this).css('cursor','pointer');
                                $(this).mousedown(function(){
                                    inputBox.val($(this).text());
                                    inputBox.keyup();
                                });
                            }else
                                $(this).remove();
                        });
                        showDiv();
                    }else{
                        hideDiv();
                    }
                });
            }else{ //if backspace
                hideDiv();
            }
        }
    });
    return this;

    function showDiv(){
        if( inputBox.val() != "" && (searchDiv.html() != "" &&
                                $(searchDiv).find("li").length > 0) )
            searchDiv.css('display','block');
        else
            hideDiv();
    }
    function hideDiv(){
        //Interval so the click om a item registers
        searchDiv.css('display','none');
    }
}